import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { View, Text, ListView } from 'react-native'
import { List } from 'antd-mobile'
import * as Libs from '../../libs/Libs'
import { RefreshListView, RefreshState } from '../../widget'
import HTTP from '../../libs/HTTP'
import { getOrganNo } from '../../libs/Libs'
import { storageRecordDetailList } from '../../libs/api'
import ScanCodeCell from '../common/ScanCodeCell'

const Item = List.Item

export default class extends Component {
  static navigationOptions = ({ navigation }) => ({
    title: '出库明细',
    headerTintColor: 'white',
    headerStyle:{ backgroundColor: '#39C0E7'}
  })

  constructor(props) {
    super(props)
    let ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 })
    this.state = {
      currentPage: 0, //当前页
      dataSource: ds.cloneWithRows([]),　//数据源
      innerData: [], //内部数据
      isLoading: false,
    }
  }

  componentDidMount() {
    this.listView && this.listView.startHeaderRefreshing()
  }

  render() {
    return (
      <View style={{flex: 1}}>
        <RefreshListView
          ref={(e) => this.listView = e}
          defaultState={RefreshState.Idle}
          dataSource={this.state.dataSource}
          renderHeader={() => {
          }}
          renderRow={(rowData) => {
            console.log('rowData ', rowData)
            return (
              <ScanCodeCell producerOrganNo={rowData['producerOrganNo']} key={rowData['erCode']} code={rowData['erCode']}></ScanCodeCell>
            )
          }}
          onHeaderRefresh={() => this.requestData()}
          onFooterRefresh={() => this.requestData()}
        />
      </View>
    )
  }

  requestData = () => {
    const params = this.props.navigation.state.params || {};
    const batchNo = params['batchNo']
    const operatorId = params['operatorId']
    console.log('batchNo: {}', batchNo)
    this.setState({isLoading: true})
    HTTP.post(storageRecordDetailList(), {
      dealerOrganNo: getOrganNo(),
      batchNo: batchNo,
      operatorId: operatorId,
      page: this.state.currentPage
    }, () => {
      this.setState({isLoading: false})
    }, (resp) => {
      if(resp.data && resp.data.length > 0 ){
        this.setState(preState => {
          const newD = preState.innerData.concat(resp['data'])
          return {
            innerData: newD,
            dataSource: preState.dataSource.cloneWithRows(newD),
            currentPage: preState.currentPage + 1
          }
        })
        this.listView.endRefreshing(RefreshState.Idle)
      } else {
        this.listView.endRefreshing(RefreshState.NoMoreData)
        return false;
      }
    })
  }

}
